package com.example.error;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2024-03-03
 * Time: 13:42
 *
 * 全局异常处理，都会走此类
 */

//@ControllerAdvice // 返回视图（转发和重定向）
@RestControllerAdvice // 返回 Json
public class GlobalExceptionHandler {

    @ExceptionHandler(ArithmeticException.class) // 精准处理算数异常
    public Object arithmeticExceptionHandle(ArithmeticException e) {
        return e.getMessage();
    }

    @ExceptionHandler(Exception.class) // 处理剩余的全部异常
    public Object exceptionHandle(Exception e) {
        return e.getMessage();
    }
}
